import adminApi from '@/api/admin';

const state = {
    adminInfoList: {},
    assignRoles: []
}

const mutations = {
    SET_ADMININFOLIST(state, adminInfoList){
        state.adminInfoList = adminInfoList;
    },
    SET_ASSIGNROLES(state, assignRoles){
        state.assignRoles = assignRoles;
    }
}

const actions = {
    // 1. 获取用户管理分页列表
    async getAdminInfoList({commit}, {page, limit, username}){
        try {
            const result = await adminApi.getAdminInfo(page, limit, username);
            // console.log(result);
            if(result.code === 20000){
                commit('SET_ADMININFOLIST', result.data);
                return result.data.total;
            }else{
                return Promise.reject(new Error('faild'));
            }
        } catch (error) {
            return Promise.reject(new Error('faild'));
        }
    },
    
}

const getters = {
    items(state){
        return (state.adminInfoList || {}).items || []
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}